package com.calsoft.Hops_Mobile_Tablet.component;
/*
 * User: Minh Diep
 * Date: 6/25/13
 * Time: 4:17 PM
 */


import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.calsoft.Hops_Mobile_Tablet.R;
import com.calsoft.Hops_Mobile_Tablet.ServiceOverViewActivity;
import com.calsoft.Hops_Mobile_Tablet.base.OpToursItemData;
import com.calsoft.Hops_Mobile_Tablet.sql.OpToursDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.SharedPrefs;

public class ServiceOverViewFragment extends Fragment {

    private String landServiceCode = "";
    private OpToursDbAdapter mDbHelper;
    private ImageView serviceImageIv;
    private TextView serviceTypeTv;
    private TextView serviceTitleTv;
    private TextView servicePriceTv;
    private TextView serviceInfoTv;
    private TextView serviceLocationTv;
    private SharedPrefs sharedPrefs;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_service_overview, container, false);
        sharedPrefs = new SharedPrefs(getActivity().getApplicationContext());
        serviceImageIv = (ImageView) rootView.findViewById(R.id.service_overview_image_iv);
        serviceTypeTv = (TextView) rootView.findViewById(R.id.service_overview_type_tv);
        serviceTitleTv = (TextView) rootView.findViewById(R.id.service_overview_title);
        servicePriceTv = (TextView) rootView.findViewById(R.id.service_overview_price);
        serviceLocationTv = (TextView) rootView.findViewById(R.id.service_overview_location);
        serviceInfoTv = (TextView) rootView.findViewById(R.id.service_overview_info);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();

        landServiceCode = getArguments().getString(ServiceOverViewActivity.SERVICE_OVER_VIEW_LAND_SERVICE_CODE, "");
        if (landServiceCode.length() > 0)
            sharedPrefs.putString(SharedPrefs.CURRENT_LAND_SERVICE_CODE, landServiceCode);
        else
            landServiceCode = sharedPrefs.getString(SharedPrefs.CURRENT_LAND_SERVICE_CODE, "");
        mDbHelper = new OpToursDbAdapter(getActivity());
        mDbHelper.open();
        Cursor cursor = mDbHelper.fetchServiceByLandServiceCode(landServiceCode);
        cursor.moveToFirst();
        OpToursItemData item = new OpToursItemData(cursor);
        cursor.close();

        serviceTypeTv.setText(item.getServiceType());
        serviceTitleTv.setText(item.getDisplayName());
        servicePriceTv.setText("From $" + item.getPrice());
        serviceInfoTv.setText(item.getServiceName());

        if (item.getCity().equals("CNS")){
            serviceLocationTv.setText("LOCATION: CAIRNS");
        } else {
            serviceLocationTv.setText("LOCATION: GOLD COAST");
        }

        String imageName = item.getImageTag();
        int drawableImageId = getActivity().getResources().getIdentifier(imageName, "drawable", getActivity().getPackageName());
        serviceImageIv.setImageResource(drawableImageId);
    }

    @Override
    public void onPause() {
        super.onPause();
        mDbHelper.close();
    }
}
